finally { Statements ---------- }
class demo { public static void main(String args[]) { System.out.println(" before call" ); try{ test(8,2); } catch(ArithmeticException er) { System.out.println("error message : "+er); } System.out.println(" after call" ); System.out.println(" program end" ); } public static void test( int a , int b ) { int c=0; open_Connection( ); System.out.println(" before expression" ); c= a/b; System.out.println(" after expression" ); close_Connection( ); System.out.println(" Result is :"+c ); } }
Before call Connection Opened Before expression After expression Connection Closed Result is 4 After call Program end
class demo { public static void main(String args[]) { System.out.println(" before call" ); try{ test(8,0); } catch(ArithmeticException er) { System.out.println("error message : "+er); } System.out.println(" after call" ); System.out.println(" program end" ); } public static void test( int a , int b ) { int c=0; open_Connection( ); System.out.println(" before expression" ); c= a/b; System.out.println(" after expression" ); close_Connection( ); System.out.println(" Result is :"+c ); } }
Before call Connection Opened Before expression Error message: Arithmetc Exception . . . After call Program end
class demo { public static void main(String args[]) { System.out.println(" before call" ); try{ test(8,2); } catch(ArithmeticException er) { System.out.println("error message : "+er); } System.out.println(" after call" ); System.out.println(" program end" ); } public static void test( int a , int b ) { int c=0; try { open_Connection( ); System.out.println(" before expression" ); c= a/b; System.out.println(" after expression" ); } finally { close_Connection( ); } System.out.println(" Result is :"+c ); } }
Before call Connection Opened Before expression After expression Connection Closed Result is 4 After call Program end
class demo { public static void main(String args[]) { System.out.println(" before call" ); try{ test(8,2); } catch(ArithmeticException er) { System.out.println("error message : "+er); } System.out.println(" after call" ); System.out.println(" program end" ); } public static void test( int a , int b ) { int c=0; try { open_Connection( ); System.out.println(" before expression" ); c= a/b; System.out.println(" after expression" ); } finally { close_Connection( ); } System.out.println(" Result is :"+c ); } }
Before call Connection Opened Before expression After expression Connection Closed Result is 4 After call Program end